package com.serial.comm.core.telegram.utils;

public final class AftnTransferMsgUtil {
  private static final String TAG_END = "NNNN";
  
  private static final String KEY_SPLIT_LINE = "\r\n";
  
  public static String TransferMsgForKe(String message) {
    if (message == null || message.length() == 0)
      return message; 
    if (!message.contains("FFM/") && !message.contains("FHL/") && !message.contains("FWB/"))
      return message; 
    if (!message.contains("/KE") && !message.contains("MBI/180"))
      return message; 
    if (!message.contains("OSI/") && !message.contains("TXT/"))
      return message; 
    String[] messageSplitList = message.split("\r\n");
    if (messageSplitList == null || messageSplitList.length == 0)
      return message; 
    StringBuilder stringBuilder = new StringBuilder();
    for (int index = 0; index < messageSplitList.length; index++) {
      if ("NNNN".equals(messageSplitList[index])) {
        stringBuilder.append(messageSplitList[index]);
      } else if (!messageSplitList[index].contains("OSI/") && !messageSplitList[index].contains("TXT/")) {
        stringBuilder.append(messageSplitList[index]);
        stringBuilder.append("\r\n");
      } else if (index + 1 == messageSplitList.length) {
        stringBuilder.append(messageSplitList[index]);
      } else if (!messageSplitList[index + 1].contains("/") && !"NNNN".equals(messageSplitList[index + 1])) {
        stringBuilder.append(messageSplitList[index]);
        stringBuilder.append(messageSplitList[index + 1]);
        stringBuilder.append("\r\n");
        index++;
      } else {
        stringBuilder.append(messageSplitList[index]);
        stringBuilder.append("\r\n");
      } 
    } 
    return stringBuilder.toString();
  }
}
